package com.wwwarehouse.financialcenter.adapter.moneyhouse;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.financialcenter.R;
import com.wwwarehouse.financialcenter.bean.bill.BillRecordResponseBean;

import java.util.List;

/**
 * @author Joseph on 2017/9/6.
 *         <p/>
 */

public class BillPagersAdapter
        extends RecyclerView.Adapter<BillPagersAdapter.ImportOrdersPagersHolder> {

    private Context mContext = null;
    private List<BillRecordResponseBean.BillRecordDetailsBean> mList = null;
    private int mHeight = 0;
    private OnItemClickListener mListener = null;

    public BillPagersAdapter(Context context, List<BillRecordResponseBean.BillRecordDetailsBean> list, int height) {
        this.mContext = context;
        this.mList = list;
        this.mHeight = height;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }

    @Override
    public ImportOrdersPagersHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ImportOrdersPagersHolder(
                View.inflate(mContext, R.layout.financial_center_bill_adapter, null));
    }

    @Override
    public void onBindViewHolder(ImportOrdersPagersHolder holder, final int position) {
        BillRecordResponseBean.BillRecordDetailsBean billDetailsBean = mList.get(position);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.itemLayout.getLayoutParams();
        params.height = (mHeight - ConvertUtils.dip2px(mContext, 35)) / 3;  //35等于条目间距 + RecyclerView顶部和底部边距

        holder.name.setText(billDetailsBean.getOtherSideName());

        if ("in".equals(billDetailsBean.getInOrOut())) {
            holder.amount.setTextColor(mContext.getResources().getColor(R.color.common_color_c1_337cff));
            holder.amount.setText(billDetailsBean.getBillAmount());
        } else if ("out".equals(billDetailsBean.getInOrOut())) {
            holder.amount.setTextColor(mContext.getResources().getColor(R.color.common_color_c2_f7a82f));
            holder.amount.setText(billDetailsBean.getBillAmount());
        } else {

        }

        holder.state.setText(billDetailsBean.getPaySatusDesc());

        if ("1".equals(billDetailsBean.getPaySatus())) {
            // 未出账
            holder.state.setTextColor(mContext.getResources().getColor(R.color.common_color_c1_337cff));
        } else if ("2".equals(billDetailsBean.getPaySatus())) {
            // 未收款/未付款
            holder.state.setTextColor(mContext.getResources().getColor(R.color.common_color_c1_337cff));
        } else if ("4".equals(billDetailsBean.getPaySatus())) {
            // 已收款 / 已付款
        } else {

        }

        holder.mode.setText(billDetailsBean.getPayTypeDesc());

        holder.date.setText(billDetailsBean.getPlanPayTime());

        if ("1".equals(billDetailsBean.getIsOverdue())) {
            // 逾期
            holder.mode.setTextColor(mContext.getResources().getColor(R.color.common_color_c3_ffffff));
            holder.date.setTextColor(mContext.getResources().getColor(R.color.common_color_c3_ffffff));

            holder.redLayout.setBackgroundColor(mContext.getResources().getColor(R.color.common_color_c11_ff1f1f));
        } else if ("0".equals(billDetailsBean.getIsOverdue())) {
            // 未逾期
        } else {

        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.ItemClickListener(position, v, "");
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public interface OnItemClickListener {
        void ItemClickListener(int position, View view, String data);
    }

    class ImportOrdersPagersHolder extends RecyclerView.ViewHolder {
        TextView name;
        TextView amount;
        StateButton  state;
        TextView mode;
        TextView date;
        LinearLayout redLayout ;
        RelativeLayout itemLayout;

        ImportOrdersPagersHolder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.title);
            amount = (TextView) itemView.findViewById(R.id.rs_earning);
            state= (StateButton) itemView.findViewById(R.id.btn_state);
            mode = (TextView) itemView.findViewById(R.id.mode);
            date = (TextView) itemView.findViewById(R.id.date);
            redLayout = (LinearLayout) itemView.findViewById(R.id.red_ground);
            itemLayout = (RelativeLayout) itemView.findViewById(R.id.rl_item);
        }
    }
}
